package com.silverbox.sweeporder.common.util;

import java.util.HashMap;
import java.util.Map;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayOpenAuthTokenAppQueryRequest;
import com.alipay.api.response.AlipayOpenAuthTokenAppQueryResponse;

/**
 * 查询授权信息
 * 
 * @author Administrator
 *
 */
public class OpenAuthTokenAppQuery {
	/** 支付宝网关 */
	public static final String ALIPAY_GATEWAY = "https://openapi.alipay.com/gateway.do";
	public static final String PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANkQr05EI7zEuZbBnA6pbDhGFvpRrfQEypBC/k5QRzA3zvhHaHHOP8BJzrRHxSaAkoUWrs48XS1eogUBlq8TC0GMq8VzvzVJNP0PZWXmegjiSfApaYXRe8VwmtXarpLofsMoTSSj5hHLzFqxxEZoLyVuL8xIryqzqyOKg5zn4a97AgMBAAECgYBhpbnXMZwKDU2X3h0AE4NVhWJM1XW1snoVj62rOT3tGQ4Ibsczex7+t7U/MJImEezX5i5ZJJqeEFY+fkp1E9IZpJS63TkL3TBRiq0fVUlM4NCvGWrWoZbZXodooHFjHbRtbieZdKacQBV438t1OmfRyoMgU3VMu3qHB/k99d2KkQJBAPV60/5exvrnJa+L1KlBlC61gRLTpNCm/5H4eESacXrrkBM+gWff+csiDqH5+47s9uf7ooyRgLpHC+2dpwJT4JcCQQDiXh5MnkN+zqR7Wkm4YOI7LnVbTiwOw3wjBFHzs5OWCwBZfV64qa27FHe+r4WMeNUeU5tFqXrwWXWhg/HFbiC9AkEAhL8XRMcb8zJsPrHvHlhIOboNQpEQ34RvUBB9eRvLoh0gnEeA0xtbiW2xXAZC8tdR7cE19a35XsJswOIwHiIdoQJBAJDF1Ob3iP8+tZ9WfTN4cveanN/KPQ8sIuZaSvdrcAZT/uM89SlxC3VMf9DqfKT8WoDMw89ntwKklEC6HBRCfJkCQC5fsPGJ0ueWMZ5ayjcg8gzmroaACqW2gEtxCTvvyp+/CqGpUZlBgAfDm/h1yR0GQtDMG0cwrsbb9WNzkTI46QQ=";
	public static final String ALIPAY_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI6d306Q8fIfCOaTXyiUeJHkrIvYISRcc73s3vF1ZT7XN8RNPwJxo8pWaJMmvyTn9N4HQ632qJBVHf8sxHi/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmdssymZkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB";
	/** 字符编码-传递给支付宝的数据编码 */
	public static final String CHARSET = "GBK";

	public static Map<String, Object> execute(String appId, String authToken) {
		AlipayClient alipayClient = new DefaultAlipayClient(ALIPAY_GATEWAY, appId, PRIVATE_KEY, "json", CHARSET,
				ALIPAY_PUBLIC_KEY);

		AlipayOpenAuthTokenAppQueryRequest request = new AlipayOpenAuthTokenAppQueryRequest();
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			request.setBizContent("{\"app_auth_token\":\"" + authToken + "\"}");
			AlipayOpenAuthTokenAppQueryResponse response = alipayClient.execute(request);
			if (null != response && response.isSuccess()) {
				System.out.println("消息发送成功 : response = " + response.getBody());
				map.put("success", "true");
				map.put("authStart", response.getAuthStart());
				map.put("authEnd", response.getAuthEnd());
				map.put("status", response.getStatus());
			} else {
				System.out.println("消息发送失败 code=" + response.getCode() + "  msg=" + response.getSubMsg());
				map.put("success", "false");
				map.put("msg", response.getSubMsg());
			}
		} catch (AlipayApiException e) {
			System.out.println("消息发送失败");
			map.put("success", "false");
			map.put("msg", "支付宝API异常");
		}
		return map;
	}

}
